package com.quyou.funtour.task;

import com.quyou.funtour.enummmeration.ActivityStatus;
import com.quyou.funtour.po.ActivityPo;
import com.quyou.funtour.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Configuration
@EnableScheduling
public class ActivityTask {

    @Autowired
    private ActivityService activityService;

    //@Scheduled(cron = "0 0 0/1 * * ? ")//每小时执行一次
    @Scheduled(cron = "0/30 0/10 * * * ?")//十分钟执行一次
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        System.out.println("执行定时任务修改活动状态");
        List<ActivityPo> list = activityService.list();
        for (ActivityPo activityPo : list) {
            Date endTime = activityPo.getEndTime();
            Date orderTime = activityPo.getOrderTime();
            if(endTime.getTime()<new Date().getTime()){
                //活动已经结束
                //结束时间小于当前时间
                activityPo.setStatus(ActivityStatus.OVER);
                activityService.updateById(activityPo);
            }else if(orderTime.getTime()<new Date().getTime()){
                //预约已经结束
                //预约时间小于当前时间
                activityPo.setStatus(ActivityStatus.UNDERWAY);
                activityService.updateById(activityPo);
            }else {
                activityPo.setStatus(ActivityStatus.PROGRESSING);
                activityService.updateById(activityPo);
            }

        }
    }

}
